'use strict';

module.exports = app => {
  const { STRING, INTEGER, JSON, TEXT, DECIMAL, BOOLEAN, DATE, ENUM } = app.Sequelize;

  const AIRecommendation = app.model.define('AIRecommendation', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      comment: '用户ID'
    },
    type: {
      type: ENUM('outfit', 'occasion'),
      allowNull: false,
      comment: '推荐类型'
    },
    occasion: {
      type: STRING(100),
      allowNull: true,
      comment: '场合类型'
    },
    weather_info: {
      type: JSON,
      allowNull: true,
      comment: '天气信息'
    },
    clothing_ids: {
      type: JSON,
      allowNull: false,
      comment: '推荐的衣服ID列表'
    },
    ai_provider: {
      type: STRING(50),
      allowNull: false,
      comment: 'AI提供商'
    },
    ai_analysis: {
      type: TEXT('long'),
      allowNull: true,
      comment: 'AI原始分析结果'
    },
    recommendation_data: {
      type: JSON,
      allowNull: false,
      comment: '完整的推荐数据'
    },
    style_tags: {
      type: JSON,
      allowNull: true,
      comment: '风格标签'
    },
    reason: {
      type: TEXT,
      allowNull: true,
      comment: '推荐理由'
    },
    score: {
      type: DECIMAL(3, 2),
      allowNull: false,
      defaultValue: 0.8,
      comment: '推荐评分'
    },
    user_rating: {
      type: INTEGER,
      allowNull: true,
      comment: '用户评分 1-5'
    },
    user_feedback: {
      type: TEXT,
      allowNull: true,
      comment: '用户反馈'
    },
    is_favorite: {
      type: BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: '是否收藏'
    },
    usage_count: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '使用次数'
    },
    last_used_at: {
      type: DATE,
      allowNull: true,
      comment: '最后使用时间'
    }
  }, {
    tableName: 'ai_recommendations',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      {
        fields: ['user_id']
      },
      {
        fields: ['type']
      },
      {
        fields: ['occasion']
      },
      {
        fields: ['user_id', 'type']
      },
      {
        fields: ['created_at']
      },
      {
        fields: ['score']
      },
      {
        fields: ['is_favorite']
      }
    ]
  });

  // 关联关系
  AIRecommendation.associate = function() {
    // 关联用户
    AIRecommendation.belongsTo(app.model.User, {
      foreignKey: 'user_id',
      as: 'user'
    });
  };

  return AIRecommendation;
};
